<template>
  <div>
    <!-- Header 区域 -->
    <van-nav-bar fixed :title="artObj.title" left-arrow @click-left="$router.back()" />

    <!-- 文章信息区域 -->
    <div class="article-container">
      <!-- 文章标题 -->
      <h1 class="art-title">{{artObj.aut_name}}</h1>

      <!-- 用户信息 -->
      <van-cell center :title="artObj.aut_name" :label="formatDate(artObj.pubdate)">
        <template #icon>
          <img :src="artObj.aut_photo" alt="" class="avatar">
        </template>
        <template #default>
          <div>
            <van-button type="info" size="mini" v-if="artObj.is_followed" @click="followedFn">已关注</van-button>
            <van-button icon="plus" type="info" size="mini" plain v-else @click="followedFn">关注</van-button>
          </div>
        </template>
      </van-cell>

      <!-- 分割线 -->
      <van-divider></van-divider>

      <!-- 文章内容 -->
      <div class="art-content" v-html="artObj.content"></div>

      <!-- 分割线 -->
      <van-divider>End</van-divider>

      <!-- 点赞 -->
      <div class="like-box">
        <van-button icon="good-job" type="danger" size="small" v-if="artObj.attitude === 1" @click="likeFn(true)">已点赞</van-button>
        <van-button icon="good-job-o" type="danger" plain size="small" v-else @click="likeFn(false)">点赞</van-button>
      </div>
    </div>
    <CommentList></CommentList>
  </div>
</template>

<script>
import { detailAPI, userFollowedAPI, userUnFollowedAPI, likeArticleAPI, unlikeArticleAPI } from '@/api'
import { timeAgo } from '@/utils/date.js'
import CommentList from './CommentList.vue'
export default {
  name: 'Detail',
  data () {
    return {
      artObj: {}
    }
  },
  async created () {
    const res = await detailAPI({
      artId: this.$route.query.art_id
    })
    console.log(res)
    this.artObj = res.data.data
  },
  methods: {
    formatDate: timeAgo,
    followedFn () {
      this.artObj.is_followed = !this.artObj.is_followed
      if (this.artObj.is_followed) {
        userFollowedAPI({ aut_id: this.artObj.aut_id })
      } else {
        userUnFollowedAPI({ aut_id: this.artObj.aut_id })
      }
    // 值为true调用关注  false调用取关
    },
    likeFn (deel) {
      if (deel) {
        //   取消
        this.artObj.attitude = -1

        unlikeArticleAPI({ artId: this.$route.query.art_id })
      } else {
        //   点赞
        this.artObj.attitude = 1
        likeArticleAPI({ artId: this.$route.query.art_id })
        // console.log(this.$route.query.art_id)
      }
    }
  },
  components: {
    CommentList
  }
}
</script>

<style scoped lang="less">
.article-container {
  padding: 10px;
  margin-top: 46px;
}
.art-title {
  font-size: 16px;
  font-weight: bold;
  margin: 10px 0;
}

.art-content {
  font-size: 12px;
  line-height: 24px;
  width: 100%;
  overflow-x: scroll;
  word-break: break-all;
    /deep/ img{
    width: 100%;
}
    /deep/ pre {
        white-space: pre-wrap;
        word-wrap: break-word;
    }
}

.van-cell {
  padding: 5px 0;
  &::after {
    display: none;
  }
}

.avatar {
  width: 60px;
  height: 60px;
  border-radius: 50%;
  background-color: #f8f8f8;
  margin-right: 5px;
  border: none;
}

.like-box {
  display: flex;
  justify-content: center;
}
</style>
